﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZEFIT
{
    public partial class RegisterForm : Form
    {
        private Controller controller = null;
        private User user = null;
        private LoginForm _lfLogin = null;
        private UsersForm _IfUsers = null;

        public RegisterForm(LoginForm lf)
        {
            this._lfLogin = lf;
            InitializeComponent();
            controller = new Controller(new MYSQLAdabterUser());
        }

        public RegisterForm(UsersForm lf, User user)
        {
            this._IfUsers = lf;
            this.user = user;
            InitializeComponent();
            controller = new Controller(new MYSQLAdabterUser());
            txb_FirstName.Text = user.FirstName;
            txb_LastName.Text = user.LastName;
            txb_UserName.Text = user.UserName;
            txb_Email.Text = user.Email;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            if (_lfLogin != null) _lfLogin.logout();
            else if (_IfUsers != null) _IfUsers.back();
            this.Close();
        }

        private void btn_register_Click(object sender, EventArgs e)
        {
            lbl_prename_error.Visible = false;
            lbl_name_error.Visible = false;
            lbl_username_error.Visible = false;
            lbl_password_error.Visible = false;
            lbl_confirm_error.Visible = false;
            lbl_mail_error.Visible = false;

            String firstName = txb_FirstName.Text.Trim();
            String lastName = txb_LastName.Text.Trim();
            String userName = txb_UserName.Text.Trim();
            String password1 = txb_Passwort1.Text.Trim();
            String password2 = txb_Passwort2.Text.Trim();
            String email = txb_Email.Text.Trim();
            if (user == null) user = new User();

            // ===========================================
            //Eingabenkontrolle
            bool test = false;
            foreach (List<object> tempList in new Controller(new MYSQLAdabterUser()).Select())
            {
                if (userName == (String)tempList[3] && user.Id == 0)
                {
                    lbl_username_error.Text = "Schon vorhanden oder " + lbl_username_error.Text;
                    lbl_username_error.Visible = true;
                    test = true;
                }
            }
            if (firstName.Equals(""))
            {
                lbl_prename_error.Visible = true;
                test = true;
            }
            if (lastName.Equals(""))
            {
                lbl_name_error.Visible = true;
                test = true;
            }
            if (userName.Equals("") || userName.Length < 6)
            {
                lbl_username_error.Visible = true;
                test = true;
            }
            if (password1.Equals(""))
            {
                lbl_password_error.Visible = true;
                test = true;
            }
            if (!password2.Equals(password1))
            {
                lbl_confirm_error.Visible = true;
                test = true;
            }
            if (email.Equals("") && !email.Contains("@"))
            {
                lbl_mail_error.Visible = true;
                test = true;
            }
            if (test) return;
            // ===========================================

            if (firstName != user.FirstName) user.FirstName = firstName;
            if (lastName != user.LastName) user.LastName = lastName;
            
            if (userName != user.UserName) user.UserName = userName;
            if (password1 != "")
            {
                if (password1 == password2) user.Password = password1;
            }
            if (email != user.Email) user.Email = email;
            user.IsActive = true;
            controller.Save(user);
            if (_lfLogin != null) _lfLogin.logout();
            else if (_IfUsers != null) _IfUsers.back();
            this.Dispose();
        }

        void DataWindow_Closing(object sender, CancelEventArgs e)
        {
            if (_lfLogin != null) _lfLogin.logout();
            else if (_IfUsers != null) _IfUsers.back();
            this.Dispose();
        }

    }
}
